﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;

namespace PlanningGIS.ArcGIS
{
    /// <summary>
    /// 对象扩展
    /// </summary>
    public static class ObjectExtension
    {
        /// <summary>
        /// DataTable扩展，添加列
        /// </summary>
        /// <param name="table"></param>
        /// <param name="fields"></param>
        /// <param name="extentname">扩展一个指定名称的文本列</param>
        public static void AddColumns(this DataTable table, IFields fields, string extentname = "")
        {
            int count = fields.FieldCount;
            for (int i = 0; i < count; i++)
            {
                IField field = fields.get_Field(i);
                DataColumn col = new DataColumn();
                switch (field.Type)
                {
                    case esriFieldType.esriFieldTypeDate:
                        col.DataType = typeof(DateTime);
                        break;
                    case esriFieldType.esriFieldTypeDouble:
                        col.DataType = typeof(decimal);
                        break;
                    case esriFieldType.esriFieldTypeSingle:
                        col.DataType = typeof(double);
                        break;
                    case esriFieldType.esriFieldTypeSmallInteger:
                    case esriFieldType.esriFieldTypeInteger:
                    case esriFieldType.esriFieldTypeOID:
                        col.DataType = typeof(int);
                        break;
                    default:
                        col.DataType = typeof(string);
                        break;
                }
                col.ColumnName = field.Name;
                col.Caption = field.AliasName;
                table.Columns.Add(col);
            }
            if (!string.IsNullOrWhiteSpace(extentname))
            {
                DataColumn col = new DataColumn();
                col.DataType = typeof(string);
                col.ColumnName = extentname;
                col.Caption = extentname;
                table.Columns.Add(col);
            }
        }

        /// <summary>
        /// 判断字符串是否与指定的要素类的名称相同
        /// </summary>
        /// <param name="value">字符串</param>
        /// <param name="equalname">要素类的名称</param>
        /// <returns></returns>
        public static bool EqualFeatureClassName(this string value, string equalname)
        {
            if (string.IsNullOrWhiteSpace(value) || string.IsNullOrWhiteSpace(equalname)) return false;
            value = value.Trim();
            equalname = equalname.Trim();
            if (value.Equals(equalname, StringComparison.CurrentCultureIgnoreCase)) return true;
            int idx1 = value.IndexOf(".");
            int idx2 = equalname.IndexOf(".");
            if (idx1 > 0 && idx2 > 0) return false;
            else if (idx1 < 0 && idx2 < 0) return false;
            if (idx1 > 0) value = value.Substring(idx1 + 1);
            if (idx2 > 0) equalname = equalname.Substring(idx2 + 1);
            return value.Equals(equalname, StringComparison.CurrentCultureIgnoreCase);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="aimArray"></param>
        /// <param name="array"></param>
        public static void Add(this IArray aimArray, IArray array)
        {
            if (array == null || array.Count == 0) return;
            for (int i = 0; i < array.Count; i++)
            {
                aimArray.Add(array.get_Element(i));
            }
        }
    }

}
